//
// Created by Lenovo on 2020/9/30.
//
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll sp[10];
int main()
{
   ll t;
   scanf("%lld",&t);
   int kcase = 0;
   while(t--)
   {
       ll ans = 0;
       for(int i = 1;i<=5;i++) scanf("%lld",&sp[i]);
       ll n;
       scanf("%lld",&n);
       for(int i = 1;i<=n;i++)
       {
           ll x;
           scanf("%lld",&x);
           if(x<=48) ans += sp[1];
           else if(x>48&&x<=56) ans += sp[2];
           else if(x>56&&x<=60) ans += sp[3];
           else if(x>60&&x<=62) ans += sp[4];
           else ans += sp[5];
       }
       printf("Case #%d: %lld0000\n",++kcase,ans);
   }
}
